package com.example.deepseek.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;

import lombok.extern.slf4j.Slf4j;

/**
 * WebSocket消息控制器
 * 处理WebSocket相关的消息传递
 */
@Controller
@Slf4j
public class WebSocketController {

    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    /**
     * 处理保持连接消息
     * 用于保持WebSocket连接活跃
     */
    @MessageMapping("/chat.keepAlive")
    public void handleKeepAlive(@Payload Object message) {
        // 仅记录日志，不需要回复
        log.debug("收到保持连接消息");
    }

    /**
     * 发送系统通知
     * 用于广播系统消息到特定会话
     */
    public void sendSystemNotification(String sessionId, String message) {
        log.info("发送系统通知到会话 {}: {}", sessionId, message);
        messagingTemplate.convertAndSend("/topic/chat/" + sessionId, 
                new SystemNotification(message));
    }

    /**
     * 系统通知内部类
     */
    static class SystemNotification {
        private String role = "system";
        private String content;

        public SystemNotification(String content) {
            this.content = content;
        }

        public String getRole() {
            return role;
        }

        public String getContent() {
            return content;
        }
    }
}